package com.junsom;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Lambda 表达式
 *
 * Created by hejunsong on 2016-8-19.
 */
public class LambdaDemo {

    /**
     * 自定义Lambda接口
     */
    public void selfLambda() {
        String a = "自定义Lambda接口";
        final FunctionalInterfaceDemo stringObjectFunction = (String b) -> {
            return b + ", hello world!";
        };
        System.out.println(stringObjectFunction.someoneSayHello(a));
    }

    /**
     * function Lambda 接口
     *
     */
    public void functionLambda() {
        String a = "function Lambda 接口";
        final Function<String, String> sayFun = (String b) -> {
            return b + ", hello world!";
        };
        System.out.println(sayFun.apply(a));
    }

    /**
     * 布尔 Lambda 接口
     *
     */
    public void predicateLambda() {
        String a = "";
        final Predicate<String> pla = (String b) -> {
            return true;
        };
        System.out.println("布尔 Lambda 接口：" + pla.test(a));
    }

    /**
     * 消费 lambda 接口
     *
     */
    public void consumerLambda() {
        Map<String, String> a = new HashMap<>();
        a.put("say", "消费 lambda 接口");

        final Consumer<Map<String, String>> conla = (Map<String, String> b) -> {
            b.put("say", b.get("say") + ", success");
        };
        conla.accept(a);
        System.out.println(a.get("say"));
    }
}
